package com.sdp.irrigation.kafka.biz.impl;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSONObject;
import com.sdp.irrigation.cache.EqRealTimeCache;
import com.sdp.irrigation.db.irrigation_eq_log_register.service.IIrrigationEqLogRegisterService;
import com.sdp.irrigation.db.irrigation_eq_real_time.service.IIrrigationEqRealTimeService;
import com.sdp.irrigation.entity.IrrigationEqLogRegister;
import com.sdp.irrigation.entity.IrrigationEqRealTime;
import com.sdp.irrigation.kafka.biz.IProtocolBiz;
import com.sdp.irrigation.protocol.CommPackage;
import com.sdp.irrigation.utils.BeanUtils;
import com.sdp.irrigation.utils.CommConstants;
import com.sdp.irrigation.utils.IrrigationConstants;
import com.sdp.irrigation.utils.JsonUtils;

@Component
public class LoginBiz implements IProtocolBiz {

	@Autowired
	private IIrrigationEqLogRegisterService eqLogRegisterService;
	
	@Autowired 
	private IIrrigationEqRealTimeService eqRealTimeService;
	
	@Autowired
	private EqRealTimeCache eqRealTimeCache;
	
	private void copyFieldValue(IrrigationEqLogRegister eqLogRegister, CommPackage commPackage) {
		JSONObject body = commPackage.getBody();
		if (body != null && body.containsKey("longitude")) {
			eqLogRegister.setEqLng(body.getString("longitude"));
		}
		if (body != null && body.containsKey("latitude")) {
			eqLogRegister.setEqLat(body.getString("latitude"));
		}
		if (body != null && body.containsKey("model")) {
			eqLogRegister.setEqModel(body.getString("model"));
		}
		if (body != null && body.containsKey("mcuId")) {
			eqLogRegister.setCcId(body.getString("mcuId"));
		}
		if (body != null && body.containsKey("hdVer")) {
			eqLogRegister.setHdVer(body.getString("hdVer"));
		}
		if (body != null && body.containsKey("sofVer")) {
			eqLogRegister.setSofVer(body.getString("sofVer"));
		}
		if (body != null && body.containsKey("wellcode")) {
			eqLogRegister.setWellCode(body.getString("wellcode"));
		}
		if (body != null && body.containsKey("timeStamp")) {
			 //获取上报时间，协议中是数字类型
			eqLogRegister.setUploadTime(CommConstants.pakSn2LocalDateTime(String.valueOf(body.getLong("timeStamp"))));
//			eqLogRegister.setUploadTime(Instant.ofEpochMilli(body.getLong("timeStamp")).atZone(ZoneId.systemDefault()).toLocalDateTime());
		}
		if (body != null && body.containsKey("xingzhengBcd12")) {
			eqLogRegister.setAreaCode(body.getString("xingzhengBcd12"));
		}
		if (body != null && body.containsKey("shebeimiyao32")) {
			eqLogRegister.setEqRegisterNo(body.getString("shebeimiyao32"));
		}
		if (body != null && body.containsKey("qiyongshengpingtai")) {
			eqLogRegister.setProvincePlatformStatus(body.getString("qiyongshengpingtai"));
		}
		if (body != null && body.containsKey("xiertai")) {
			eqLogRegister.setXiertaiUrl(body.getString("xiertai"));
		}
		if (body != null && body.containsKey("shengpingtai")) {
			eqLogRegister.setProvincePlatformUrl(body.getString("shengpingtai"));
		}
		if (body != null && body.containsKey("xiangmufang")) {
			eqLogRegister.setProjectUrl(body.getString("xiangmufang"));
		}
		if (body != null && body.containsKey("ceshi")) {
			eqLogRegister.setTestUrl(body.getString("ceshi"));
		}
		if (body != null && body.containsKey("sanfang")) {
			eqLogRegister.setSanfangUrl(body.getString("sanfang"));
		}
		if (body != null && body.containsKey("yunwei")) {
			eqLogRegister.setSanfangUrl(body.getString("yunwei"));
		}
	}
	
	@Override
	public void execute(List<CommPackage> packageList) {
		if (packageList.size() > 0) {
			List<IrrigationEqLogRegister> insertList = new ArrayList<>();
			Map<String, IrrigationEqRealTime> eqRealTimeMap = new HashMap<>();
		
			for (CommPackage commPackage : packageList) {
				String devSn = CommConstants.getLoginCommPackageDevSn(commPackage);
				if (!StringUtils.isEmpty(devSn)) {
					IrrigationEqLogRegister eqLogRegister = new IrrigationEqLogRegister();
					eqLogRegister.setEqNo(devSn);
					copyFieldValue(eqLogRegister, commPackage);
					eqLogRegister.setCreateTime(LocalDateTime.now());
					eqLogRegister.setUpdateTime(LocalDateTime.now());
					eqLogRegister.setProtocolContent(JsonUtils.toJSONString(commPackage));
					
					IrrigationEqRealTime eqRealTime = eqRealTimeCache.getEqRealTimeCache(devSn);
					if (eqRealTime != null && !StringUtils.isEmpty(eqRealTime.getEqNo())) {
						//eqLogRegister.setEqName(eqRealTime.get);
//						eqLogRegister.setEqType(eqRealTime);
//						eqLogRegister.setProjectNo(eqRealTime.get);
//						eqLogRegister.setFieldNo(eqRealTime);
						eqLogRegister.setPwNo(eqLogRegister.getWellCode());

						//更新缓存的信息
						//设备上报的时间不准，优先使用服务器的更新时间
						eqRealTime.setUpdateTime(eqLogRegister.getUpdateTime());
						eqRealTime.setOnlineStatus(IrrigationConstants.设备在线状态_在线.getCode());
						eqRealTime.setPwNo(eqLogRegister.getWellCode());
						eqRealTime.setMcuId(eqLogRegister.getMcuId());
						eqRealTime.setCcId(eqLogRegister.getCcId());
						eqRealTime.setHdVer(eqLogRegister.getHdVer());
						eqRealTime.setSofVer(eqLogRegister.getSofVer());
						eqRealTime.setEqLat(eqLogRegister.getEqLat());
						eqRealTime.setEqLng(eqLogRegister.getEqLng());
						eqRealTime.setAreaCode(eqLogRegister.getAreaCode());
						eqRealTime.setEqRegisterNo(eqLogRegister.getEqRegisterNo());
						eqRealTime.setProvincePlatformStatus(eqLogRegister.getProvincePlatformStatus());
						eqRealTime.setXiertaiUrl(eqLogRegister.getXiertaiUrl());
						eqRealTime.setProvincePlatformUrl(eqLogRegister.getProvincePlatformUrl());
						eqRealTime.setProjectUrl(eqLogRegister.getProjectUrl());
						eqRealTime.setTestUrl(eqLogRegister.getTestUrl());
						eqRealTime.setSanfangUrl(eqLogRegister.getSanfangUrl());
						eqRealTime.setYunweiUrl(eqLogRegister.getYunweiUrl());
						
						//新建对象，对缓存进行部分更新
						IrrigationEqRealTime eqRealTimeUpdate = new IrrigationEqRealTime();
						eqRealTimeUpdate.setUpdateTime(eqRealTime.getUpdateTime());
						eqRealTimeUpdate.setOnlineStatus(IrrigationConstants.设备在线状态_在线.getCode());						
						eqRealTimeUpdate.setPwNo(eqLogRegister.getWellCode());
						eqRealTimeUpdate.setMcuId(eqLogRegister.getMcuId());
						eqRealTimeUpdate.setCcId(eqLogRegister.getCcId());
						eqRealTimeUpdate.setHdVer(eqLogRegister.getHdVer());
						eqRealTimeUpdate.setSofVer(eqLogRegister.getSofVer());
						eqRealTimeUpdate.setEqLat(eqLogRegister.getEqLat());
						eqRealTimeUpdate.setEqLng(eqLogRegister.getEqLng());
						eqRealTimeUpdate.setAreaCode(eqLogRegister.getAreaCode());
						eqRealTimeUpdate.setEqRegisterNo(eqLogRegister.getEqRegisterNo());
						eqRealTimeUpdate.setProvincePlatformStatus(eqLogRegister.getProvincePlatformStatus());
						eqRealTimeUpdate.setXiertaiUrl(eqLogRegister.getXiertaiUrl());
						eqRealTimeUpdate.setProvincePlatformUrl(eqLogRegister.getProvincePlatformUrl());
						eqRealTimeUpdate.setProjectUrl(eqLogRegister.getProjectUrl());
						eqRealTimeUpdate.setTestUrl(eqLogRegister.getTestUrl());
						eqRealTimeUpdate.setSanfangUrl(eqLogRegister.getSanfangUrl());
						eqRealTimeUpdate.setYunweiUrl(eqLogRegister.getYunweiUrl());
						
						eqRealTimeCache.updateEqRealTimeCache(devSn, BeanUtils.redisBean2Map(eqRealTimeUpdate));
						
						//更新数据库信息
						if (!eqRealTimeMap.containsKey(devSn)) {
							eqRealTimeMap.put(devSn, eqRealTime);
						}
					}
					
					insertList.add(eqLogRegister);
					
				}
				
			}
			if (eqRealTimeMap.size() > 0) {
				eqRealTimeService.updateBatchById(new ArrayList<>(eqRealTimeMap.values()));
			}
			
			if (insertList.size() > 0) {
				eqLogRegisterService.saveBatch(insertList);
			}

		}

	}
}
